//
//  ArticoliScegliVC.m
//  iTeVe
//
//  Created by Pedro Gonzalez on 01/03/14.
//  Copyright (c) 2014 SISI Software. All rights reserved.
//

#import "ArticoliScegliVC.h"
#import "FMDBDataAccess.h"
#import "ArticoloCell.h"
#import "Utility.h"


@interface ArticoliScegliVC ()

@end




@implementation ArticoliScegliVC


@synthesize delegate;
@synthesize isScelgoArticolo;
@synthesize searchArticoli;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    searchArticoli.delegate = (id)self;
    searchArticoli.placeholder = self.title = NSLocalizedString(@"CERCA_ARTICOLO", @"Testo Cerca...");
    [self populateArticoli:@""];
    
}


-(void)searchBar:(UISearchBar*)searchClienti textDidChange:(NSString*)text
{
    if(text.length > 0){
        [self populateArticoli:text];
    } else {
        [self populateArticoli:@""];
    }
    
    [self.tableView reloadData];
}


-(void) populateArticoli:(NSString *)filtro
{
    articoli = [[NSMutableArray alloc] init];
    
    FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
    
    articoli = [db getArticoli:filtro];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [articoli count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ArticoloCell";
    
    ArticoloCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Articolo *articolo = [articoli objectAtIndex:[indexPath row]];
    cell.codice.text = articolo.codart;
    cell.descrizione.text = articolo.descrizione;
    cell.aliquota.text = articolo.codali;
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    // Find the correct index of cell should scroll to.
    int foundIndex = 0;
    for (Articolo *obj in articoli) {
        if ([[[obj.descrizione substringToIndex:1] uppercaseString] compare:title] == NSOrderedSame || [[[obj.descrizione substringToIndex:1] uppercaseString] compare: title] == NSOrderedDescending)
            break;
        foundIndex++;
    }
    if(foundIndex >= [articoli count])
        foundIndex = (int)[articoli count]-1;
    
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:foundIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    return 1;
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Articolo *articolo = [articoli objectAtIndex:[indexPath row]];
    [self.delegate selectItemViewController:self didFinishEnteringItem:articolo];
    
}



@end
